package com.zxmr.domain.service;

import java.sql.SQLException;
import java.util.List;

import com.zxmr.CommonInit;
import com.zxmr.db.DBManager;
import com.zxmr.domain.Comment;
import com.zxmr.domain.Opportunity;
import com.zxmr.domain.OpportunityComments;

public class OpportunityService {

	DBManager db = CommonInit.getDBManager();
	
	public boolean addOpportunity(Opportunity opp)
	{
		try
		{	
			db.getMapper().insert("opportunity.add", opp);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public Opportunity queryOpportunity(long id)
	{
		try
		{
			Opportunity opp = (Opportunity)db.getMapper().queryForObject("opportunity.findByID", id);
			return opp;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Opportunity> queryOpportunities(int compay)
	{
		try
		{
			return (List<Opportunity>)db.getMapper().queryForList("opportunity.findByCompany", compay);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean updateOpportunity(Opportunity opp)
	{
		try
		{
			db.getMapper().update("opportunity.update",opp);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public List<Opportunity> queryOpportunityPagination(OpportunitySearchBean searchBean)
	{
		try
		{
			return (List<Opportunity>)db.getMapper().queryForList("opportunity.findByPagination", searchBean);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public int queryOpportunityCount(OpportunitySearchBean searchBean)
	{
		try
		{
			return (Integer)db.getMapper().queryForObject("opportunity.findCountByPagination", searchBean);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return -1;
	}
	
	public boolean addComment(Comment comment,long opportunity)
	{
		try {
			db.getMapper().startTransaction();
			long commentId = (Long)db.getMapper().insert("comment.add", comment);
			comment.setId(commentId);
			
			OpportunityComments cc = new OpportunityComments();
			cc.setComment(commentId);
			cc.setOpportunity(opportunity);
			db.getMapper().insert("opportunity.addComment", cc);
			db.getMapper().commitTransaction();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();

		}finally{
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		
		return false;
	}
	public boolean deleteComment(OpportunityComments cc)
	{
		try {
			db.getMapper().startTransaction();
			db.getMapper().delete("opportunity.deleteComment", cc);
			db.getMapper().delete("comment.delete", cc.getComment());
			db.getMapper().commitTransaction();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();

		}finally{
			
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	public boolean deleteComment(long comment)
	{
		try {
			OpportunityComments cc = (OpportunityComments)db.getMapper().queryForObject("opportunity.queryOpportunityCommentByComment", comment);
			db.getMapper().startTransaction();
			db.getMapper().delete("opportunity.deleteComment", cc);
			db.getMapper().delete("comment.delete", comment);
			db.getMapper().commitTransaction();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			
		}finally{
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	@SuppressWarnings("unchecked")
	public List<Comment> queryAllComments(long opportunity)
	{
		try {
			return (List<Comment>)db.getMapper().queryForList("opportunity.queryAllComments", opportunity);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public Comment queryComment(long id)
	{
		try {
			return (Comment)db.getMapper().queryForObject("comment.findByID", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
